package com.example.demo.game;

import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketSession;

import java.util.concurrent.ConcurrentHashMap;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Wukong
 * Date: 2023-04-24
 * Time: 20:10
 */
@Component
public class OnlineUserManager {
    //成员变量
    //集合：表示 “游戏大厅” 场景，存储等待匹配或未进入游戏房间的用户会话
    private ConcurrentHashMap<Integer, WebSocketSession> gameHall = new ConcurrentHashMap<>();
    //表示 “游戏房间” 场景，存储正在游戏中的用户会话
    private ConcurrentHashMap<Integer, WebSocketSession> gameRoom = new ConcurrentHashMap<>();
    //进入游戏大厅
    public void enterGameHall(int userId, WebSocketSession session) {
        gameHall.put(userId, session);
    }

    //离开游戏大厅
    public void exitGameHall(int userId) {
        gameHall.remove(userId);
    }
   //获取游戏大厅中的用户会话
    public WebSocketSession getSessionFromGameHall(int userId) {
        return gameHall.get(userId);
    }
   //进入游戏房间
    public void enterGameRoom(int userId, WebSocketSession session) {
        gameRoom.put(userId, session);
    }
   //离开游戏房间
    public void exitGameRoom(int userId) {
        gameRoom.remove(userId);
    }
   //获取游戏房间中的用户会话
    public WebSocketSession getSessionFromGameRoom(int userId) {
        return gameRoom.get(userId);
    }
}